package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Dish;
import com.itheima.bean.Setmeal;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.exception.CustomException;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/setmeal")
@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //新增套餐
    @CacheEvict(value = "setmeal", allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        //1.调用service处理
        setmealService.saveWithDish(setmealDto);

        //2.响应数据
        return R.success("新增成功！");
    }

    //分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> findPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, String name) {
        //1.调用service处理 返回Page<SetmealDto>
        Page<SetmealDto> setmealDtoPage = setmealService.findPage(page, pageSize, name);

        //2.响应数据
        return R.success(setmealDtoPage);
    }

    @CacheEvict(value = "setmeal", allEntries = true)
    //套餐删除
    @DeleteMapping
    public R<String> deleteByIds(Long[] ids) {
        //1.调用service处理
        setmealService.deleteByIds(ids);

        //2.响应数据
        return R.success("删除成功！");
    }

    //根据分类id查询套餐列表数据
    @Cacheable(value = "setmeal", key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        //1.调用service处理
        //1.1：创建wrapper对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();

        //1.2：设置查询条件 分类id和status
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());

        //1.3：调用service中的list方法查询 传入wrapper对象
        List<Setmeal> list = setmealService.list(wrapper);

        //2.响应数据
        return R.success(list);
    }

    /**
     * 套餐修改回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> updateEcho(@PathVariable Long id) {
        //调用service层
        //响应数据
        return setmealService.updateEcho(id);
    }

    @CacheEvict(value = "setmeal", allEntries = true)
    /**
     * 套餐修改保存
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        //判断前端获得的数据不为空
        if (setmealDto == null) {
            throw new CustomException("数据传输异常");
        }
        //调用service层
        return setmealService.updateWithSetmealDish(setmealDto);
    }

    /**
     * 套餐启用或者禁用||批量
     *
     * @param status
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmeal", allEntries = true)
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        //设置查询条件

        //调用service
        for (Long id : ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            boolean save = setmealService.saveOrUpdate(setmeal);
            if (!save) {
                throw new CustomException("修改" + setmeal.getName() + "状态失败");
            }

        }
        //响应数据
        return R.success("修改成功");
    }
}
